package com.ytjj.qmyx.supplychain.service;

import com.github.pagehelper.PageHelper;
import com.ytjj.qmyx.supplychain.common.api.CommonPage;
import com.ytjj.qmyx.supplychain.common.exception.ApiException;
import com.ytjj.qmyx.supplychain.mapper.BankerStoreAddressMapper;
import com.ytjj.qmyx.supplychain.mapper.entity.YxBanker;
import com.ytjj.qmyx.supplychain.mapper.entity.YxBankerStoreAddress;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class BankerStoreAddressService {
    @Autowired
    private BankerService bankerService;
    @Autowired
    private BankerProductService bankerProductService;
    @Resource
    private BankerStoreAddressMapper bankerStoreAddressMapper;

    public int insertAll(YxBankerStoreAddress yxBankerStoreAddress) {
        //获取bankerId
        YxBanker userInfo = bankerService.getUserInfo();
        yxBankerStoreAddress.setBankerId(userInfo.getId());
        yxBankerStoreAddress.setDefaultFlag((byte) 0);
        int insertAll = bankerStoreAddressMapper.insert(yxBankerStoreAddress);
        return insertAll;
    }

    public List<YxBankerStoreAddress> selectAll(Integer bankerId) {
        YxBankerStoreAddress yxBankerStoreAddress = new YxBankerStoreAddress();
        yxBankerStoreAddress.setBankerId(bankerId);
        if (bankerId == null){
            //获取bankerId
            YxBanker userInfo = bankerService.getUserInfo();
            yxBankerStoreAddress.setBankerId(userInfo.getId());
        }
        List<YxBankerStoreAddress> addresses = bankerStoreAddressMapper.selectAddresses(yxBankerStoreAddress);
//        List<YxBankerStoreAddress> addresses = new ArrayList<>();
        if (CollectionUtils.isEmpty(addresses)){
            return new ArrayList<>();
        }
        addresses.stream().forEach(item -> {
            String pcaAddress = item.getProvince() + item.getCity() + (null == item.getArea() ? "": item.getArea()) + item.getAddress();
            item.setPCAaddress(pcaAddress);
        });
        return addresses;
    }

    public CommonPage select(Integer pageNum,
                             Integer pageSize) {
        //获取bankerId
        YxBankerStoreAddress yxBankerStoreAddress = new YxBankerStoreAddress();
        YxBanker userInfo = bankerService.getUserInfo();
        yxBankerStoreAddress.setBankerId(userInfo.getId());
        PageHelper.startPage(pageNum, pageSize);
        List<YxBankerStoreAddress> addresses = bankerStoreAddressMapper.selectAddresses(yxBankerStoreAddress);
//        List<YxBankerStoreAddress> addresses = new ArrayList<>();
        if (CollectionUtils.isEmpty(addresses)){
            return null;
        }
        addresses.stream().forEach(item -> {
            String pcaAddress = item.getProvince() + item.getCity() + (null == item.getArea() ? "": item.getArea()) + item.getAddress();
            item.setPCAaddress(pcaAddress);
        });

        CommonPage page = CommonPage.restPage(addresses);
        return page;
    }

    public int updateById(YxBankerStoreAddress yxBankerStoreAddress) {
        return bankerStoreAddressMapper.updateByPrimaryKeySelective(yxBankerStoreAddress);
    }

    public int deleteAll(Integer id) {
        //获取bankerId
        YxBanker userInfo = bankerService.getUserInfo();
        //查询是否有关联商品
        int count = bankerProductService.selectByAddressIdAndBankerId(id,userInfo.getId());
        //如果有关联商品就不让他删除地址
        if (count > 0){
            throw new ApiException("已有商品关联退货地址,不能删除");
        }
        return bankerStoreAddressMapper.deleteByPrimaryKey(id);
    }

    public int updateDefaultAddress(Integer id, Byte defaultFlag) {
        YxBanker userInfo = bankerService.getUserInfo();
        Example example = new Example(YxBankerStoreAddress.class);
        example.createCriteria().andEqualTo("defaultFlag", defaultFlag).andEqualTo("bankerId", userInfo.getId());
        List<YxBankerStoreAddress> list = bankerStoreAddressMapper.selectByExample(example);
        list.stream().forEach(item -> {
            item.setDefaultFlag(new Byte("0"));
            bankerStoreAddressMapper.updateByPrimaryKey(item);
        });
        YxBankerStoreAddress bean = bankerStoreAddressMapper.selectByPrimaryKey(id);
        if (null == bean) {
            return 0;
        }
        bean.setDefaultFlag(defaultFlag);
        return bankerStoreAddressMapper.updateByPrimaryKey(bean);
    }
}
